package liuyuyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import liuyuyang.domain.Classes;
import liuyuyang.domain.Course;
import liuyuyang.domain.Student;
import liuyuyang.domain.StudentCourse;
import liuyuyang.feign.ClassesFeign;
import liuyuyang.feign.CourseFeign;
import liuyuyang.feign.StudentCourseFeign;
import liuyuyang.mapper.StudentMapper;
import liuyuyang.pojo.Result;
import liuyuyang.service.StudentService;
import liuyuyang.vo.StudentVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private ClassesFeign classesFeign;
    @Resource
    private CourseFeign courseFeign;
    @Resource
    private StudentCourseFeign studentCourseFeign;

    @Override
    public String editOradd(Student student) {
        try {
            if (student.getSid() != null && student.getSid() != 0) {
                // 先删除该学生的所有课程
                studentCourseFeign.del(student.getSid());
                // 编辑数据
                studentMapper.updateById(student);
            } else {
                // 新增数据
                studentMapper.insert(student);
            }

            // 关联课程
            for (Integer id : student.getCourseIds()) {
                StudentCourse studentCourse = new StudentCourse();
                studentCourse.setSid(student.getSid());
                studentCourse.setCid(id);

                // 远程调用进行关联
                studentCourseFeign.add(studentCourse);
            }

            return "操作成功";
        } catch (Exception e) {
            return "操作失败";
        }
    }

    @Override
    public String del(Integer id) {
        String result = studentMapper.deleteById(id) == 1 ? "删除成功" : "删除失败";
        return result;
    }

    @Override
    public Student get(Integer id) {
        Student student = studentMapper.selectById(id);

        // 关联班级
        Result<Classes> classes = classesFeign.get(student.getSid());
        student.setClasses(classes.getData());

        // 关联课程
        Result<List<Course>> course = courseFeign.get(student.getSid());
        student.setCourseList(course.getData());
        student.setCourseIds(course.getData().stream().map(Course::getCid).collect(Collectors.toList()));

        return student;
    }

    @Override
    public Page<Student> list(StudentVo studentVo, Integer num, Integer size) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();

        // 查班级
        if (studentVo.getClassId() != null && studentVo.getClassId() != 0) {
            queryWrapper.eq("c_id", studentVo.getClassId());
        }

        // 查名称
        if (studentVo.getSname() != null && !studentVo.getSname().isEmpty()) {
            queryWrapper.like("sname", "%" + studentVo.getSname() + "%");
        }

        // 查出生时间范围
        if (studentVo.getStartDate() != null && studentVo.getEndDate() != null) {
            queryWrapper.between("birthday", studentVo.getStartDate(), studentVo.getEndDate());
        } else if (studentVo.getStartDate() != null) {
            queryWrapper.ge("birthday", studentVo.getStartDate());
        } else if (studentVo.getEndDate() != null) {
            queryWrapper.le("birthday", studentVo.getEndDate());
        }

        // 分页查询
        Page<Student> page = new Page<>(num, size);
        studentMapper.selectPage(page, queryWrapper);

        // 关联数据
        for (Student student : page.getRecords()) {
            // 关联班级
            Result<Classes> classes = classesFeign.get(student.getSid());
            student.setClasses(classes.getData());

            // 关联课程
            Result<List<Course>> course = courseFeign.get(student.getSid());
            student.setCourseList(course.getData());
            student.setCourseIds(course.getData().stream().map(Course::getCid).collect(Collectors.toList()));
        }

        return page;
    }
}
